<template>
  <div class="app-container">
    <!-- 主注册区域 -->
    <div class="register-container">
      <!-- 注册表单卡片 -->
      <div class="register-card">
        <div class="register-header">
          <h2>用户注册</h2>
          <p>填写详细信息完成注册</p>
        </div>

        <form @submit.prevent="handleRegister">
          <div class="form-group">
            <label for="username">用户名</label>
            <div class="input-with-icon">
              <i class="fas fa-user" />
              <input
                id="username"
                v-model="username"
                type="text"
                placeholder="设置您的用户名"
                maxlength="10"
              >
            </div>
            <div class="input-hint">
              用户名不能超过10个字符
            </div>
          </div>

          <div class="form-group">
            <label for="account">账号</label>
            <div class="input-with-icon">
              <i class="fas fa-id-card" />
              <input
                id="account"
                v-model="account"
                type="text"
                placeholder="用于登录平台的账号"
              >
            </div>
          </div>

          <div class="form-group">
            <label for="password">密码</label>
            <div class="input-with-icon">
              <i class="fas fa-lock" />
              <input
                id="password"
                v-model="password"
                type="password"
                placeholder="设置您的密码"
                maxlength="20"
              >
            </div>
            <div class="input-hint">
              密码长度建议在8-20个字符之间，包含字母和数字
            </div>
          </div>

          <button
            type="submit"
            class="btn-submit"
            data-cy="btn-submit"
          >
            注册新账号
          </button>
        </form>

        <div class="login-link">
          已有账号？<router-link to="/login">
            立即登录
          </router-link>
        </div>
      </div>
    </div>
  </div>
</template>

<script>
import { userAPI } from '@/api/user';

export default {
  name: 'RegisterPage',
  data() {
    return {
      username: '',
      account: '',
      password: ''
    }
  },
  methods: {
    async handleRegister() {
      if(!this.username || !this.account || !this.password){
        alert('请填写完整信息（用户名、账号和密码）');
        return;
      }
      if (this.username.length > 10) {
        alert('用户名不能超过10个字符');
        return;
      }
      if (this.password.length > 20) {
        alert('密码不能超过20个字符');
        return;
      }
      if (this.password.length < 8) {
        alert('密码长度需至少8个字符');
        return;
      }

      const user = {
        userName: this.username,
        userAccount: this.account,
        userPassword: this.password
      }

      try {
        const response = await userAPI.register(user);
        console.log('注册成功', response.data);
        alert('注册成功，请登录');
        this.$router.push('/login');
      } catch(error) {
        console.error('注册失败', error);
        this.username = "";
        this.account = "";
        this.password = "";
        alert('注册失败，请检查信息是否符合要求');
      }
    }
  }
}
</script>

<style scoped>
* {
  margin: 0;
  padding: 0;
  box-sizing: border-box;
  font-family: 'Segoe UI', 'Microsoft YaHei', sans-serif;
}

.app-container {
  min-height: 100vh;
  background: #f5f7fa;
}

/* 垂直居中容器 */
.register-container {
  display: flex;
  justify-content: center;
  align-items: center;
  min-height: 100vh;
  padding: 20px;
}

.register-card {
  background: white;
  border-radius: 12px;
  box-shadow: 0 10px 30px rgba(0, 0, 0, 0.12);
  padding: 40px 50px;
  width: 100%;
  max-width: 500px;
  animation: fadeInUp 0.6s ease-out forwards;
  position: relative;
  overflow: hidden;
}

/* 装饰性元素 */
.register-card::before {
  content: '';
  position: absolute;
  top: 0;
  left: 0;
  right: 0;
  height: 6px;
  background: linear-gradient(90deg, #4facfe 0%, #00f2fe 100%);
}

/* 注册标题样式 */
.register-header {
  text-align: center;
  margin-bottom: 30px;
  position: relative;
}

.register-header h2 {
  font-size: 28px;
  font-weight: 700;
  color: #202124;
  margin-bottom: 10px;
}

.register-header p {
  font-size: 16px;
  color: #5f6368;
  opacity: 0.8;
}

/* 表单样式优化 */
.form-group {
  margin-bottom: 24px;
}

.form-group label {
  display: block;
  font-size: 14px;
  font-weight: 500;
  color: #5f6368;
  margin-bottom: 8px;
}

.input-with-icon {
  position: relative;
}

.input-with-icon i {
  position: absolute;
  left: 15px;
  top: 15px;
  color: #888;
  font-size: 18px;
}

.input-with-icon input {
  width: 100%;
  padding: 14px 14px 14px 48px;
  font-size: 16px;
  border: 1px solid #dadce0;
  border-radius: 8px;
  background-color: #f8f9fa;
  transition: all 0.3s;
  height: 52px;
}

.input-with-icon input:focus {
  outline: none;
  border-color: #4facfe;
  box-shadow: 0 0 0 3px rgba(79, 172, 254, 0.15);
  background-color: #fff;
}

.input-hint {
  font-size: 13px;
  color: #888;
  margin-top: 8px;
  line-height: 1.4;
}

/* 按钮优化 */
.btn-submit {
  display: block;
  width: 100%;
  padding: 16px;
  font-size: 16px;
  font-weight: 600;
  color: white;
  background: linear-gradient(135deg, #1a73e8 0%, #0d47a1 100%);
  border: none;
  border-radius: 8px;
  cursor: pointer;
  transition: all 0.3s;
  margin-top: 15px;
  box-shadow: 0 4px 12px rgba(26, 115, 232, 0.25);
  letter-spacing: 1px;
}

.btn-submit:hover {
  background: linear-gradient(135deg, #1669c1 0%, #0a2c5e 100%);
  box-shadow: 0 6px 15px rgba(26, 115, 232, 0.35);
  transform: translateY(-2px);
}

/* 登录链接 */
.login-link {
  text-align: center;
  margin-top: 25px;
  font-size: 15px;
  color: #666;
  padding-top: 15px;
  border-top: 1px solid #f0f0f0;
}

.login-link a {
  color: #1a73e8;
  text-decoration: none;
  font-weight: 500;
  transition: all 0.2s;
  margin-left: 5px;
}

.login-link a:hover {
  color: #0d47a1;
  text-decoration: underline;
}

/* 动画效果 */
@keyframes fadeInUp {
  from {
    opacity: 0;
    transform: translateY(30px);
  }
  to {
    opacity: 1;
    transform: translateY(0);
  }
}

/* 响应式设计 */
@media (max-width: 600px) {
  .register-card {
    padding: 30px 25px;
    box-shadow: 0 5px 15px rgba(0, 0, 0, 0.08);
  }

  .register-header h2 {
    font-size: 24px;
  }

  .input-with-icon i {
    font-size: 16px;
    top: 16px;
    left: 12px;
  }

  .input-with-icon input {
    padding: 12px 12px 12px 40px;
    height: 48px;
  }
}
</style>
